package liuliu.custom.control.edittext;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.LinearLayout;

import liuliu.custom.R;

/**
 * 房屋户型
 * Created by Administrator on 2016/5/13.
 */
public class HomeTypeEditText extends LinearLayout {
    EditText shi_et;
    EditText ting_et;
    EditText wei_et;

    public HomeTypeEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    /**
     * 加载组件
     *
     * @param context
     */
    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.et_home_type_layout, this, true);//自定义控件绑定
        shi_et = (EditText) findViewById(R.id.shi_num_et);
        ting_et = (EditText) findViewById(R.id.ting_num_et);
        wei_et = (EditText) findViewById(R.id.wei_num_et);
        initEvents();
    }

    /**
     * 加载页面事件
     */
    private void initEvents() {
        shi_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!shi_et.getText().toString().trim().equals("")) {
                    shi_et.clearFocus();
                    ting_et.requestFocus();
                }
            }
        });
        ting_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!ting_et.getText().toString().trim().equals("")) {
                    ting_et.clearFocus();
                    wei_et.requestFocus();
                }
            }
        });
    }

    public HomeTypeEditText(Context context) {
        this(context, null);
        init(context);
    }

    public HomeTypeEditText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        init(context);
    }

    /**
     * 获得户型信息
     *
     * @return 户型信息
     */
    public String getHouseStyle() {
        String val = "";
        if (shi_et.getText().toString() != "") {
            val += shi_et.getText().toString() + "室";
        } else {
            val += "0室";
        }
        if (ting_et.getText().toString() != "") {
            val += ting_et.getText().toString() + "厅";
        } else {
            val += "0厅";
        }
        if (wei_et.getText().toString() != "") {
            val += wei_et.getText().toString() + "卫";
        } else {
            val += "0厅";
        }
        return val;
    }

    /**
     * 清空户型信息
     */
    public void clearHouseStyle() {
        shi_et.setText("");
        ting_et.setText("");
        wei_et.setText("");
    }
}

